package com.tcwlkj.uuService.NetWork;

import android.content.Intent;
import android.support.annotation.CallSuper;
import android.util.Log;

import com.tcwlkj.uuService.MyApp;
import com.tcwlkj.uuService.model.CommonResponse;
import com.tcwlkj.uuService.model.TokenModel;
import com.tcwlkj.uuService.ui.login.LoginActivity;
import com.tcwlkj.uuService.utils.ChcheUtils;
import com.tcwlkj.uuService.utils.EventCenter;
import com.tcwlkj.uuService.utils.NetUtils;
import com.tcwlkj.uuService.utils.PreferencesUtils;
import com.tcwlkj.uuService.utils.ToastUtil;
import com.tcwlkj.uuService.utils.Tools;
import com.tcwlkj.uuService.utils.WaitDialog;
import com.tcwlkj.uuService.utils.WaitDialogUtil;

import org.greenrobot.eventbus.EventBus;

import java.net.ConnectException;
import java.net.SocketTimeoutException;

import cn.jpush.android.api.JPushInterface;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/10/13 0013.
 */

public abstract class HttpCallBack<T> implements Callback<T> {
    private final int RESPONSE_CODE_FAILED = -1; //返回数据失败
    @Override
    public void onResponse(Call<T> call, Response<T> response) {
        WaitDialogUtil.getInstance().dismissDialog();
        CommonResponse<T> commonResponse = (CommonResponse<T>) response.body();
        if (commonResponse!=null&&commonResponse.isSuccess()) {
            Log.d("commonResponse",commonResponse.toString());
            if (commonResponse.getStatus() == 1) {
                onSuccess(commonResponse);
            } else {
                onFail(commonResponse.getStatus(),  commonResponse.getStatus()+":"+commonResponse.getMessage());
            }
        } else {
            Log.d("commonResponse",response.code()+"");
            if(response.code()==200){
                if(commonResponse.getStatus()==40102||commonResponse.getStatus()==40100||commonResponse.getStatus()==40111){
                    if(!PreferencesUtils.getBoolean(MyApp.getContext(),"Login",false)){
                        RetrofitUtils.getOpenidRequest().gettoken("13-10-1-1")
                                .enqueue(new Callback<CommonResponse<TokenModel>>() {
                                    @Override
                                    public void onResponse(Call<CommonResponse<TokenModel>> call, Response<CommonResponse<TokenModel>> response) {
                                        if(response.body().isSuccess()){
                                            CommonResponse<TokenModel> commonResponse= (CommonResponse<TokenModel>) response.body();
                                            PreferencesUtils.putString(MyApp.getContext(),"openid",commonResponse.getData().getOpenid());
                                            PreferencesUtils.putString(MyApp.getContext(),"token",commonResponse.getData().getToken());
                                            onFail(commonResponse.getStatus(),response.message());
                                        }else {
                                            onFail(response.body().getStatus(),response.message());
                                        }
                                    }
                                    @Override
                                    public void onFailure(Call<CommonResponse<TokenModel>> call, Throwable t) {
                                        onFail(RESPONSE_CODE_FAILED,t.getMessage().toString());
                                    }
                                });
                    }else {
                        EventBus.getDefault().post(new EventCenter<String>(40111,"退出登录"));
                        onFail(commonResponse.getStatus(),"账号异常,请重新登录");
                    }
                }else if(commonResponse.getStatus()==40104){
                    RetrofitUtils.getOpenidRequest().getopenid(PreferencesUtils.getString(MyApp.getContext(),"token"))
                            .enqueue(new Callback<CommonResponse>() {
                                @Override
                                public void onResponse(Call<CommonResponse> call, Response<CommonResponse> response) {
                                    if(response.body().isSuccess()){
                                        onFail(commonResponse.getStatus(),response.message());
                                    }else {
                                        onFail(response.body().getStatus(),response.message());
                                    }
                                }
                                @Override
                                public void onFailure(Call<CommonResponse> call, Throwable t) {
                                    onFail(RESPONSE_CODE_FAILED,t.getMessage().toString());
                                }
                            });
                }else {
                    onFail(commonResponse.getStatus(), commonResponse.getMessage());
                }
            } else {
                onFail(response.code(),response.code()+":"+response.message());
            }
        }
    }
    @Override
    public void onFailure(Call<T> call, Throwable t) {
        WaitDialogUtil.getInstance().dismissDialog();
        if(!NetUtils.isConnectedAndToast(MyApp.getContext())){
            return;
        }
        String temp = t.getMessage().toString();
        String errorMessage = "获取数据失败:" + temp;
        Log.e("error", temp);
        if (t instanceof SocketTimeoutException) {
            errorMessage = "服务器响应超时";
        } else if (t instanceof ConnectException) {
            errorMessage = "网络连接异常，请检查网络";
        }
        onFail(RESPONSE_CODE_FAILED, errorMessage);
    }


    public abstract void onSuccess(Object t);

    public abstract void onFail(int code, String message);

}
